home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.kei.com!wang!news
- From: emild@cs.technion.ac.il (Kohn Emil Dan)
- Subject: Re: What's so different about 2-D arrays???
- Organization: Technion, Israel Institute of Technology
- Date: Tue, 2 Jan 1996 17:57:32 GMT
- Message-ID: <Pine.SV4.3.91-heb-2.04.960102194523.6304A-100000@cs.technion.ac.il>
- References: <820451938.20697@fredblog.demon.co.uk>
- Sender: news@wang.com
-
-
-
- On Sun, 31 Dec 1995, Mark Winfield wrote:
-
- > I have been teaching myself 'C' over the last month. Just before Xmas
- > I thought that I had it worked out and that all I had to do was
- > practice. I decided to write a program that would record chess games,
- > it would allow move branching as well. I decided upon this because it
- > should use everything I have learnt except DOS calls.
- > Now for the problem,
- >
- > //My own chess recorder program
- >
- > #include <stdio.h>
- > #include <ctype.h>
- >
- > /*Prototypes*/
- > void setup(char pos[8][8]);
- >
- >
- > void main()
- > {
- > char pos[8][8];
- >
- > setup(pos);
- >
- >
- > printf("\nWell Done!!");
- > }
- >
- > void setup(char pos[8][8])
- > {
- > int x,y;
- >
- > pos[1][8]=pos[8][8]='r';
- ^ ^ ^-------------array index to high
- > for(x=1;x<9;x++){
- > pos[x][7]='a';
- ^--------------x will reach the value 8, again
- array index to high
- > }
- >
-
- Your problem is caused by using array subscripts that exceed the maximum
- value they can get, whwn referencing an array element.
-
- In C, array subscripts (no matter how many dimensions they have), range
- from 0 to maximum_size -1, so in your problem, having an array
-
- char pos[8][8];
-
- valid array references are:
-
- pos [0][0] .. pos [0][7],
- pos [1][0] .. pos [1][7],
- ....
- pos [7][0] .. pos [7][7]
-
- Best regards,
-
- Emil
-